package cn.kooki.app.duobao.ui.Fragment.shipaddress;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseFragment;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.AddressListResponse;
import cn.kooki.app.duobao.data.Bean.User.ShipAddressItem;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.bus.CityListChangeEvent;
import cn.kooki.app.duobao.data.bus.ShipAddressEmptyEvent;
import cn.kooki.app.duobao.data.bus.ShipAddressNotEmptyEvent;
import cn.kooki.app.duobao.ui.Activity.User.AddShipAddressActivity;
import cn.kooki.app.duobao.ui.Adapter.AddressAdapter;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import cn.kooki.app.duobao.util.SharePreferencesUtils;
import de.greenrobot.event.EventBus;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/14.
 */
public class ShipAddressListFragment extends BaseFragment {
    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loading_wrapper)
    CustomRelativeLayout loadingWrapper;

    private ArrayList<ShipAddressItem> datas = new ArrayList<>();
    private AddressAdapter addressAdapter;
    private UserInfo userinfo;

    @Override
    public View setView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_ship_address_list, container, false);
        return view;
    }

    @Override
    public void afterView() {
        userinfo = SharePreferencesUtils.getUserinfo(context);
        addressAdapter = new AddressAdapter(context, datas);
        listView.setAdapter(addressAdapter);
        loadingWrapper.showloading();
        CustomRefreshHeader refreshHeader = new CustomRefreshHeader(context);
        swipeRefreshLayout.addPtrUIHandler(refreshHeader);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent(AddShipAddressActivity.class, "item", datas.get(position));
            }
        });
        getdata(false);
    }

    private void getdata(final boolean is_load_more) {
        MainFactory.getApiInstance().listaddress(userinfo.getUid(), new Callback<AddressListResponse>() {
            @Override
            public void success(AddressListResponse addressListResponse, Response response) {
                if (addressListResponse.getData() != null) {
                    if (!is_load_more) {
                        datas.clear();
                    }
                    datas.addAll(addressListResponse.getData());
                    addressAdapter.notifyDataSetChanged();
                    hideloading();
                }
                refreshView();
            }


            @Override
            public void failure(RetrofitError error) {
                hideloading();
                refreshView();
            }
        });
    }

    public void onEventMainThread(CityListChangeEvent event) {
        getdata(false);
    }

    private void hideloading() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            swipeRefreshLayout.refreshComplete();
        }
        if (loadingWrapper != null)
            loadingWrapper.hideloading();
    }

    private void refreshView() {
        if (datas.size() == 0) {
            EventBus.getDefault().post(new ShipAddressEmptyEvent());
        } else {
            EventBus.getDefault().post(new ShipAddressNotEmptyEvent());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
